package cn.gzmli.dtt.common.service;

import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.entity.UserMac;
import cn.gzmli.dtt.common.utils.BaseService;
import cn.gzmli.dtt.common.utils.Constant;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/12/25
 */
@Service
@Transactional
public class UserMacService extends BaseService<UserMac> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 检测用户的mac地址是否存在
     */
    public List<Map<String, Object>> findUserMacByUserIdAndMac(Integer userId,String pcMac){
        String sql = "SELECT * FROM user_mac um where um.userId=? and um.pcMac=?";
        return jdbcTemplate.queryForList(sql,userId,pcMac);
    }

    /**
     * 保存用户的mac地址
     */
    public Result save(Integer userId,String pcMac) throws Exception {

        List list = findUserMacByUserIdAndMac(userId,pcMac);
        if(list!=null&&list.size()>0){
            return new Result(Constant.FAIL,"该Mac地址已存在");
        }
        UserMac userMac = new UserMac();
        userMac.setUserId(userId);
        userMac.setPcMac(pcMac);
        userMac.setId((Integer) this.getDao().insert(userMac));
        return new Result(Constant.SUCCESS,"添加成功",userMac);
    }

    /**
     * 删除用户的mac地址
     */
    public Result removeUserMacById(Integer id){
        String sql = "delete from user_mac where id=?";
        jdbcTemplate.update(sql,id);
        return new Result(Constant.SUCCESS,"删除成功");
    }

    /**
     * 分页查询用户的Mac地址列表
     * @return
     * @throws Exception
     */
    public Result findUserMacByUserId(Integer userId,PageInfo pageInfo) throws Exception{

        List<Map<String, Object>> result;
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT SQL_CALC_FOUND_ROWS id,userId,pcMac FROM user_mac WHERE userId = ? ");

        if(pageInfo!=null&& StrUtil.isNotEmpty(pageInfo.getOrderData())){
            sql.append(" ORDER BY "+pageInfo.getOrderData()+" ");
        }
        if(pageInfo != null && pageInfo.getPageSize() > 0){
            sql.append(" LIMIT "+ pageInfo.getStartRow()+ " , "+pageInfo.getPageSize());
            result = jdbcTemplate.queryForList(sql.toString(),userId);
            String countSql = "SELECT FOUND_ROWS() FROM DUAL";
            pageInfo.setTotalRows(jdbcTemplate.queryForObject(countSql,Integer.class));
            pageInfo.createTotalPages();
        }else {
            result = jdbcTemplate.queryForList(sql.toString(),userId);
        }
        return new Result(Constant.SUCCESS,"查询成功",result,pageInfo);
    }

    public Set<String> getUserMacsByUserId(Integer userId) throws Exception{

        String sql = "SELECT id,userId,pcMac FROM user_mac WHERE userId = ? ";

        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql,userId);
        Set<String> set = new HashSet<>();
        result.forEach(map->{
            set.add(map.get("pcMac").toString().trim().toUpperCase());
        });

        return set;
    }


    /**
     * 修改用户mac地址
     */
    public Result updateUserMacById(Integer id,Integer userId,String pcMac) throws Exception {

        List<Map<String, Object>> list = findUserMacByUserIdAndMac(userId,pcMac);
        if(list!=null&&list.size()>0&&!(list.get(0).get("pcMac").equals(pcMac))){
            return new Result(Constant.FAIL,"该Mac地址已存在");
        }
        String sql = "update user_mac set pcMac=? where id=?";
        jdbcTemplate.update(sql,pcMac,id);
        return new Result(Constant.SUCCESS,"修改成功");
    }


}
